<template>
	<view class="forget">
		
		<view class="content">
			<!-- 主体 -->
			<view class="main">
				<view class="tips">若您忘记了密码，可在此重置新密码。</view>
				<wInput
					v-model="phone_number"
					type="text"
					maxlength="11"
					placeholder="请输入手机号码"
				></wInput>
				<wInput
					v-model="password"
					type="password"
					placeholder="请输入新密码"
					isShowPass
				></wInput>
				<wInput
					v-model="password_repetition"
					type="password"
					placeholder="请再次输入新密码"
					isShowPass
				></wInput>
				
				<wInput
					v-model="verification_code"
					type="number"
					maxlength="6"
					placeholder="验证码"
					
					isShowCode
					codeText="获取验证码"
					setTime="60"
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
			</view>
			
			<wButton 
				class="wbutton"
				text="重置密码"
				:rotate="isRotate" 
				@click.native="upPwd()"
			></wButton>

		</view>
	</view>
</template>

<script>
	var _this;
	import base from '@/common/base.js';
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	export default {
		mixins: [base],
		data() {
			return {
				phone_number: "", //电话
				password: "", //密码
				password_repetition: "", //确认密码
				verification_code:"", //验证码
				isRotate: false, //是否加载旋转
			}
		},
		components:{
			wInput,
			wButton
		},
		mounted() {
			_this= this;
		},
		methods: {
			getVerCode() {
				var phone_reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
				if (!phone_reg.test(this.phone_number)) {
					uni.showToast({
						"title": "手机号错误，请重新填写",
						"icon": "none"
					});
					return;
				}
				this.$refs.runCode.$emit('runCode');
				uni.showLoading({
					title: "发送中..."
				})
				this.$http.post('api/passport/sms-code', {
					mobile: this.phone_number,
					usage: 'up-pwd',
				}).then((res) => {
					uni.hideLoading();
					this.$config.toast('验证码发送成功');
				}).catch((error) => {
					uni.hideLoading();
					this.$refs.runCode.$emit('runCode',0);
					this.$config.toast(error.message);
				});
			},
			upPwd() {
				if(this.isRotate){
					return false;
				}
				var phone_reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
				if (!phone_reg.test(this.phone_number)) {
					this.$config.toast("手机号不正确，请重新输入");
					return;
				}
				if (this.password.length == 0) {
					this.$config.toast("请输入密码");
					return;
				}
				if (this.password_repetition.length == 0) {
					this.$config.toast("请输入确认密码");
					return;
				}
				if (this.password != this.password_repetition) {
					this.$config.toast("两次输入密码不一致");
					return;
				}
				let data = {
					mobile: this.phone_number,
					code: this.verification_code,
					password: this.password,
					password_repetition: this.password_repetition
				};
				_this.isRotate=true;
				this.$http.post('api/passport/up-pwd', data).then((res) => {
					_this.isRotate=false
					this.login(res);
					uni.navigateBack();
				}).catch((error) => {
					_this.isRotate=false
					this.$config.toast(error.message);
				});
			},
		}
	}
</script>

<style>
	@import url("@/components/watch-login/css/icon.css");
	@import url("@/static/style/login.css");
	page {
		background: #FFFFFF !important;
	}
</style>

